package com.example.demo.config;

import com.example.demo.common.ResultAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created with IntelliJ IDEA.
 * Description:统一异常处理
 * User: yyx
 * Date: 2023-11-03
 * Time: 15:48
 */
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(NullPointerException.class)
    public ResultAdvice doNPE(NullPointerException e){
        ResultAdvice resultAdvice = new ResultAdvice();
        resultAdvice.setState(-1);
        resultAdvice.setMsg("空指针异常"+e.getMessage());
        resultAdvice.setData(null);
        return resultAdvice;

    }
    @ExceptionHandler(Exception.class)
    public ResultAdvice doExceptions(Exception e){
        ResultAdvice resultAdvice = new ResultAdvice();
        resultAdvice.setState(-1);
        resultAdvice.setMsg("异常"+e.getMessage());
        resultAdvice.setData(null);
        return resultAdvice;

    }
}
